import pinyin from './convert-pinyin.js'

// Date类	开始
Date.prototype.format = function (fmt) {
    const o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (const k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
Date.prototype.lastDate = function () {
    const fullYear = this.getFullYear();
    const month = this.getMonth() + 1;
    return new Date(fullYear, month, 0);
};
Date.prototype.calendar = function () {
	let date = new Date(this.setDate(1))
	let startDay = date.getDay();
	let startDate = new Date(this.getTime() - 24 * 60 * 60 * 1000 * startDay + 24 * 60 * 60 * 1000)
	date = this.lastDate()
	let endDay = date.getDay() ? date.getDay() : 7;
	let endDate = new Date(date.getTime() + 24 * 60 * 60 * 1000 * (7-endDay))
	let calendar = []
	date = startDate
	while(endDate.getTime() > date.getTime()) {
		calendar.push(date)
		date = new Date(date.getTime() + 24 * 60 * 60 * 1000)
	}
	calendar.push(endDate)
	return calendar
}
// Date类	结束
// String类	开始
String.prototype.format = function (fmt) {
    if (!fmt || fmt === {}) {
        return this;
    }
    let string = this;
    for (const key in fmt) {
        string = string.replace(new RegExp("\\{" + key + "\\}", "g"), fmt[key])
    }
    return string;
};
String.prototype.toJSON = function () {
    try {
        return JSON.parse(this);
    } catch (e) {
        console.error("string is must be a json string")
        return {};
    }
};

String.prototype.toPinYin = function () {
    return pinyin.getFullChars(this);
};
// Date类	结束
export default {
    init() {
        console.log("*** tim proto-type ***")
    }
}